import Vue from 'vue';
import Vuex from 'vuex';

import app from './modules/app';
import user from './modules/user';

Vue.use(Vuex);

const store = new Vuex.Store({
  state : {
    currentUser : null,
    headerHeight: document.getElementsByTagName('body')[0].offsetHeight - 80
  },
  getters : {
    currentUser : state=> {
      let user = state.currentUser;
      if (!user) {
        let str = localStorage.getItem('currentUser') || null;
        if (!str) {
          user = null;
        }
        user = JSON.parse(str);
      }
      return user;
    },
    headerHeight : state => {
      return document.getElementsByTagName('body')[0].offsetHeight - 80;
    }
  },
  mutations: {
    setCurrentUser(state,user) {
      state.currentUser = user;
    },
    headerHeight(state){
      state.headerHeight = document.getElementsByTagName('body')[0].offsetHeight - 80;
    },
  },
  actions: {
    setCurrentUser (context) {
      context.commit('setCurrentUser')
    }
  },
  modules: {
    app,
    user
  }
});

export default store;
